﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using TestesFixacao.Framework.Extensions;
using TestesFixacao.Dominio.Modelos;

namespace TestesFixacao.Dominio.Readers
{
    public class QuestaoReader : IConvertible<List<Questao>>
    {
        private AlternativaReader _alternativaReader;

        public QuestaoReader(AlternativaReader alternativaReader)
        {
            _alternativaReader = alternativaReader;
        }

        public void Convert(XmlTextReader readerDisciplinas, List<Questao> questoes)
        {
            int numeroQuestoes = 0;

            var readerQuestoes = readerDisciplinas.ElementCount("questoes", "alternativa", ref numeroQuestoes);

            readerQuestoes.ReadStartElement("questoes");

            for (int i = 0; i < numeroQuestoes; i++)
            {
                var item = new Questao();

                readerQuestoes.ReadStartElement("alternativa");

                item.Enunciado = readerQuestoes.ReadElementString("enunciado");

                item.Bimestre = int.Parse( readerQuestoes.ReadElementString("bimestre") );

                item.Numero = int.Parse(readerQuestoes.ReadElementString("numero"));
                
                _alternativaReader.Convert(readerQuestoes, item.Alternativas);

                readerQuestoes.ReadEndElement();

                questoes.Add(item);
            }

            readerQuestoes.ReadEndElement();
        }
    }
}
